﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ReporterEngine.Model
{
    public class InstanceCategory : INotifyPropertyChanged
    {
        public string CategoryName { get; set; }
        public CategoryTypes CategoryType { get; set; }
        
        public string InstanceGroupTypeName { get; set; }//used for meta groups

        public InstanceNode Node { get; set; }
        List<BaseInstanceNode> _Childs;

        public CIFModel Model { get; set; }

        public List<BaseInstanceNode> Childs
        {
            get
            {
                if (_Childs == null)
                {
                    this.IsLoading = true;

                    _Childs = new List<BaseInstanceNode>();

                    switch (CategoryType)
                    {
                        case CategoryTypes.Properties:
                            _Childs.AddRange(this.Node.Model.GetInstanceProperties(Node).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.Referencies:
                            _Childs.AddRange(this.Node.Model.GetInstanceReferencies(Node).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.UsedBy:
                            _Childs.AddRange(this.Node.Model.GetInstanceUsedBy(Node).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.InstanceGroupType:
                            _Childs.AddRange(this.Model.GetInstanceNodesByTypeName(InstanceGroupTypeName).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.Members:
                            _Childs.AddRange(this.Node.Model.GetInstanceMembers(Node).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.Dimensions:
                            _Childs.AddRange(this.Node.Model.GetInstanceDimensions(Node).Cast<BaseInstanceNode>());
                            break;
                        case CategoryTypes.Custom:
                           
                            break;
                    }

                    this.IsLoading = false;
                }

                return _Childs;
            }
            set { _Childs = value; }
        }

        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                if (this._isLoading != value)
                {
                    this._isLoading = value;
                    this.NotifyPropertyChanged("IsLoading");
                }
            }
        }

        private bool _isLoading = false;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
